package com.jijuxie.common.core;

/**
 * 通用返回结果
 *
 * @author jijuxie
 */
public class R<T> {
    private int code;
    private String msg;
    private T data;

    public static final int SUCCESS = 200;
    public static final int ERROR = 500;

    private R() {
    }

    private R(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 返回成功结果
     */
    public static <T> R<T> ok() {
        return new R<>(SUCCESS, "操作成功");
    }

    /**
     * 返回成功结果
     */
    public static <T> R<T> ok(T data) {
        return new R<>(SUCCESS, "操作成功", data);
    }

    /**
     * 返回成功结果
     */
    public static <T> R<T> ok(String msg, T data) {
        return new R<>(SUCCESS, msg, data);
    }

    /**
     * 返回失败结果
     */
    public static <T> R<T> fail() {
        return new R<>(ERROR, "操作失败");
    }

    /**
     * 返回失败结果
     */
    public static <T> R<T> fail(String msg) {
        return new R<>(ERROR, msg);
    }

    /**
     * 返回失败结果
     */
    public static <T> R<T> fail(int code, String msg) {
        return new R<>(code, msg);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
